﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCControl;

namespace Cost.Controllers
{
    public class MaterialProcessStorageController : Controller
    {
        #region 构造函数
        private readonly ICostRepository<MaterialProcess> materialprocessRepository;
        private readonly ICostRepository<MaterialProcessItem> materialprocessitemRepository;
        private readonly ICostRepository<Storage> storageRepository;
        private readonly ICostRepository<StockRecord> stockrecordRepository;
        private readonly ICostRepository<Material> materialRepository;
        private const string ListState = "Picking";
        private const string State = "Storage";
        private const string StockType = "加工领料";
        public MaterialProcessStorageController(ICostRepository<MaterialProcess> materialprocessRepository,
            ICostRepository<MaterialProcessItem> materialprocessitemRepository,
            ICostRepository<Storage> storageRepository,
            ICostRepository<StockRecord> stockrecordRepository,
            ICostRepository<Material> materialRepository)
        {
            this.materialprocessRepository = materialprocessRepository;
            this.materialprocessitemRepository = materialprocessitemRepository;
            this.storageRepository = storageRepository;
            this.stockrecordRepository = stockrecordRepository;
            this.materialRepository = materialRepository;
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = materialprocessRepository.GetPageable(pager, m => m.OrderState == ListState, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult Edit(string Id)
        {
            var data = materialprocessRepository.Find(Id) ?? new MaterialProcess();
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(string Id, IList<MaterialProcessItem> MaterialList)
        {
            var data = materialprocessRepository.Find(Id) ?? new MaterialProcess();
            data.OrderState = State;
            materialprocessRepository.Save(data);

            foreach (var childitem in MaterialList)
            {
                foreach (var m in MaterialList)
                {
                    var childdata = materialprocessitemRepository.Find(m.Id);
                    childdata.ActualStorageCount = m.ActualStorageCount;
                    childdata.State = State;

                    //当入库单的状态为“入库”时 需要向库存表中写入信息
                    Storage Sordate = storageRepository.Find(s => s.MaterialId == m.MaterialId);
                    if (Sordate == null)
                    {
                        Sordate = new Storage();
                        var Material = materialRepository.Find(m.MaterialId)?? new Material();
                        Sordate.ActualSorage = m.ActualStorageCount;
                        Sordate.MaterialId = m.MaterialId;
                        Sordate.Code = m.Code;
                        Sordate.Name = m.Name;
                        Sordate.Category = m.Category;
                        Sordate.Specification = Material.Specification;
                        Sordate.Unit = Material.Unit;
                        Sordate.Type = "SemiMaterial";
                        Sordate.CurrentPrice = Material.CurrentPrice;
                        storageRepository.Insert(Sordate);
                    }
                    else
                    {
                        Sordate.ActualSorage += m.ActualStorageCount;
                        storageRepository.Save(Sordate);
                    }

                    //保存库存记录
                    StockRecord StockRecorddate = new StockRecord()
                    {
                        Id = Guid.NewGuid().ToString(),
                        MaterialId = m.MaterialId,
                        Type = StockType,
                        Count = m.ActualStorageCount,
                    };
                    stockrecordRepository.Insert(StockRecorddate);
                }
            }
            return Json(AjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult MaterialList(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = materialprocessitemRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult EditList()
        {
            return View();
        }

        [HttpPost]
        public JsonResult EditList(string orderId, nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = materialprocessitemRepository.GetPageable(pager, d => d.OrderId == orderId, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        [HttpPost]
        public JsonResult Storage(string Id)
        {
            var data = materialprocessRepository.Find(Id) ?? new MaterialProcess();
            var MaterialList = materialprocessitemRepository.FindAll(m => m.OrderId == Id).ToList();

            return Save(data.Id, MaterialList);
        }
    }
}
